package com.kuzlak.cms.module.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kuzlak.cms.bean.Result;
import com.kuzlak.cms.config.ConstantHeader;
import com.kuzlak.cms.constant.ResultConstant;
import com.kuzlak.cms.module.sys.bean.MenuVO;
import com.kuzlak.cms.module.sys.entity.SysLoginLog;
import com.kuzlak.cms.module.sys.entity.SysMenu;
import com.kuzlak.cms.module.sys.service.SysMenuService;
import com.kuzlak.cms.utils.RedisClient;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-11-17
 */
@Tag(name = "菜单管理")
@RestController
@RequestMapping("/api/menu")
public class MenuController {
    @Autowired
    private SysMenuService service;
    @Autowired
    private RedisClient redisUtil;


    @Operation(summary= "菜单列表")
    @PostMapping("list")
    public Result list(@Parameter(description= "名称") @RequestParam(name = "name", defaultValue = "") String name) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMenu::getDeleted, 0);
        queryWrapper.lambda().orderByAsc(SysMenu::getSort);
        if (StringUtils.isNotEmpty(name)) {
         //   queryWrapper.lambda().inSql(SysMenu::getId, PermissionSqlModel.serachName(name));
            //  serachName

        }
        //  queryWrapper.lambda().eq (SysMenu::getStatus,1);

        List<SysMenu> list = service.list(queryWrapper);
        return Result.ok(list);
    }

    @CacheEvict(cacheNames = "menu")
    @Operation(summary= "添加或编辑")
    @PostMapping("edit")
    public Result edit(@RequestBody@Valid MenuVO dataVo
    ) {
        System.out.println(dataVo);
        SysMenu data= BeanUtil.copyProperties(dataVo,SysMenu.class);
        Long id = data.getId();
        if (id == null) {
            data.setId(0L);
        }

        try {
            boolean flag = false;
            if (id > 0) {
                data.setId(id);
                flag = service.updateById(data);
            } else {
                flag = service.save(data);
            }
            if (flag) {
                redisUtil.delAll("menu");
            }

            return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);

        }catch (Exception e){
            return Result.failure(ResultConstant.FAIL);
        }


    }

    @Operation(summary= "权限列表")
    @PostMapping("permsList")
    public Result permsList(@Parameter(description= "名称") @RequestParam(name = "name", defaultValue = "") String name) {

 /*       SysLoginLog adminToken = ConstantHeader.getAdminToken();
        QueryWrapper <SysMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().inSql (SysMenu::getId, PermissionSqlModel.menus(adminToken.getUserId()));
        List <SysMenu> list = service.list();
        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);*/
        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        List<Map<String, Object>> byDataList = new ArrayList<>();
        String userName = adminToken.getUserName();
        if (userName.equals("admin")) {
            byDataList = service.findByDataAdminList(adminToken.getUserId(), name);
        } else {
            byDataList = service.findByDataList(adminToken.getUserId(), name);
        }

        return Result.ok(byDataList);

    }


    @Operation(summary= "权限关键字列表")
    @PostMapping("permsKeyList")
    public Result permsKeyList() {

        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        List<String> byDataList = new ArrayList<>();
        String userName = adminToken.getUserName();
        if (userName.equals("admin")) {
            byDataList = service.findByDataAdminKeyList();
        } else {
            byDataList = service.findByDataKeyList(adminToken.getUserId());
        }

        return Result.ok(byDataList);

    }

    @Operation(summary= "permsKeyDataList")
    @PostMapping("permsKeyDataList")
    public Result permsKeyDataList() {

        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        List<Map<String, Object>> byDataList = new ArrayList<>();
        String userName = adminToken.getUserName();
        if (userName.equals("admin")) {
            byDataList = service.findByDataAdminKeyDataList();
        } else {
            byDataList = service.findByDataKeyDataList(adminToken.getUserId());
        }

        return Result.ok(byDataList);

    }

    @Operation(summary= "权限Menu列表")
    @PostMapping("permsMiniList")
    public Result menuList() {

 /*       SysLoginLog adminToken = ConstantHeader.getAdminToken();
        QueryWrapper <SysMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().inSql (SysMenu::getId, PermissionSqlModel.menus(adminToken.getUserId()));
        List <SysMenu> list = service.list();
        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);*/
        SysLoginLog adminToken = ConstantHeader.getAdminToken();
        List<Map<String, Object>> byDataList = new ArrayList<>();
        String userName = adminToken.getUserName();
        if (userName.equals("admin")) {
            byDataList = service.findByDataAdminMenuList(adminToken.getUserId());
        } else {
            byDataList = service.findByDataMenuList(adminToken.getUserId());
        }
        return Result.ok(byDataList);

    }

    @Operation(summary= "状态修改")
    @PostMapping("status")
    public Result status(
            @RequestParam(name = "status", defaultValue = "0") int status,
            @RequestParam(name = "id", defaultValue = "0") long id
    ) {
        Result result = new Result();
        if (id > 0) {

            LambdaUpdateWrapper<SysMenu> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(SysMenu::getStatus, status);
            updateWrapper.eq(SysMenu::getId, id);
            boolean update = service.update(updateWrapper);
            if (update) {
                redisUtil.delAll("menu");
                result.setCode(ResultConstant.SUCSESS);
            } else {
                result.setCode(ResultConstant.FAIL);
            }
        } else {
            result.setCode(ResultConstant.EMPTIY);
        }

        return result;
    }


    @Operation(summary= "删除")
    @PostMapping("delete")
    public Result status(
            @RequestParam(name = "id", defaultValue = "0") long id
    ) {
        Result result = new Result();
        if (id > 0) {
            boolean update = service.deleteById(id);
            if (update) {

                result.setCode(ResultConstant.SUCSESS);
            } else {
                result.setCode(ResultConstant.FAIL);
            }
        } else {
            result.setCode(ResultConstant.EMPTIY);
        }

        return result;
    }
}

